package org.dromara.comm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.comm.domain.AsServicePerson;
import org.dromara.comm.domain.AsServiceType;
import org.dromara.comm.mapper.AsServicePersonMapper;
import org.dromara.comm.mapper.AsServiceTypeMapper;
import org.dromara.comm.service.AsServicePersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通讯录业务层
 * @author: 郭伟明
 * @date: 2024-08-01 15:06
 **/
@Service
public class AsServicePersonServiceImpl extends ServiceImpl<AsServicePersonMapper, AsServicePerson> implements AsServicePersonService {

    @Autowired
    private AsServicePersonMapper asServicePersonMapper;


    @Autowired
    private AsServiceTypeMapper asServiceTypeMapper;

    @Override
    public  List<Map<String, Object>> getServiceGuideJson(AsServiceType asServiceType) {
        List<Map<String, Object>> arrayList = new ArrayList<>();
        List<AsServiceType> serviceChannels = asServiceTypeMapper.selectList(null);

        for (AsServiceType serviceChannel : serviceChannels) {
            LambdaQueryWrapper<AsServicePerson> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AsServicePerson::getServiceTypeId, serviceChannel.getId());
            List<AsServicePerson> asServicePlatforms = asServicePersonMapper.selectList(wrapper);

            Map<String, Object> serviceChannelMap = new HashMap<>();
            serviceChannelMap.put("name", serviceChannel.getName());
            serviceChannelMap.put("asServicePlatforms", asServicePlatforms);

            Map<String, Object> result = new HashMap<>();
            result.put("serviceChannel", serviceChannelMap);
            arrayList.add(result);
        }
        return arrayList;
    }

    /**
     * 插入通讯录
     *
     * @param asServiceType
     */
    @Override
    public void insertAsServiceType(AsServiceType asServiceType) {
        asServiceTypeMapper.insert(asServiceType);
    }


    /**
     * 插入通讯录内容
     * @param asServicePerson
     */
    @Override
    public void insertAsServicePerson(AsServicePerson asServicePerson, AsServiceType asServiceType) {
        AsServicePerson person = new AsServicePerson();
        if (asServiceType.getId()!=null){
            person.setName(asServicePerson.getName());
            person.setDescription(asServicePerson.getDescription());
            person.setPhone(asServicePerson.getPhone());
            person.setServiceTypeId(asServiceType.getId());
            asServicePersonMapper.insert(person);
        }else {
            throw new RuntimeException("空值针异常");
        }
    }


    /**
     * 插入通讯录
     *
     * @param asServiceType
     */
    @Override
    public void updateAsServiceType(AsServiceType asServiceType) {
        asServiceTypeMapper.updateById(asServiceType);
    }

    /**
     * 插入通讯录内容
     * @param asServicePerson
     */
    @Override
    public void updateAsServicePerson(AsServicePerson asServicePerson) {
        asServicePersonMapper.updateById(asServicePerson);
    }

    /**
     * 删除通讯录
     * @param asServicePerson
     */
    @Override
    public void deleteAsServicePerson(AsServicePerson asServicePerson) {
        asServicePersonMapper.deleteById(asServicePerson.getId());
    }
}
